<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\plat\logic;

/**
 * 部门逻辑
 */
class TenDept extends PlatBase
{

    // 面包屑
    public static $crumbs = [];

    // 部门Select结构
    public static $deptSelect = [];

    /**
     * 获取部门列表
     */
    public function getTenDeptList($where = [], $field = true, $order = 'sort asc', $paginate = DB_LIST_ROWS)
    {
        return $this->modelTenDept->getList($where, $field, $order, $paginate);
    }


    /**
     * 获取部门信息
     */
    public function getTenDeptInfo($where = [], $field = true)
    {
        return $this->modelTenDept->getInfo($where, $field);
    }

    /**
     * 部门添加
     */
    public function tenDeptAdd($data = [])
    {
        $validate_result = $this->validateTenDept->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenDept->getError()];
        }
        $result = $this->modelTenDept->setInfo($data);

        $result && ten_log('新增', '新增部门，name：' . $data['name']);

        $url = url('show', ['pid' => $data['pid'] ? $data['pid'] : 0]);

        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelTenDept->getError()];
    }

    /**
     * 部门编辑
     */
    public function tenDeptEdit($data = [])
    {

        $validate_result = $this->validateTenDept->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenDept->getError()];
        }

        $url = url('show');
        $result = $this->modelTenDept->setInfo($data);
        $result && ten_log('编辑', '编辑部门，name：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelTenDept->getError()];
    }

    /**
     * 部门删除
     */
    public function tenDeptDel($where = [])
    {

        $result = $this->modelTenDept->deleteInfo($where, true);

        $result && ten_log('删除', '删除部门，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTenDept->getError()];
    }

    //得到tree的数据
    //适应树形结构
    public function getTenDeptListTree($where = [], $field = "id,name,pid", $order = 'sort asc', $paginate = false)
    {
        $list = $this->getTenDeptList($where, $field, $order, $paginate)->toArray();
        $tree = list2tree($list);
        return $tree;
    }

    //得到tree的数据
    //适应树形结构Select下拉选择
    public function getTenDeptTreeSelect($where = [], $field = "id,name,pid", $order = 'sort asc', $paginate = false)
    {
        $list = $this->getTenDeptList($where, $field, $order, $paginate)->toArray();
        $data = list2select($list);
        return $data;
    }

    /**
     * Checkbox,用于授权勾选
     *
     * @param $dataTreeList
     * @param $child
     * @param $rules_array
     * @return string
     * @author: 开发人生 goodkfrs@qq.com
     * @Time: 2025/3/9 11:36
     */
    public function tenDeptTreeCheckboxView($dataTreeList = [], $child = 'child', $rules_array = [])
    {
        $html_view = '';
        $rules_array = is_array($rules_array) ? $rules_array : explode(',', $rules_array);
        foreach ($dataTreeList as $oneInfo) {
            $icon = '';
            $checkbox_select = in_array($oneInfo['id'], $rules_array) ? "checked='checked'" : '';
            if (!empty($oneInfo[$child])) {
                $html_view .= '<li class="has_child">';
                $html_view .= "<span><i class='icon-minus-sign'></i></span>
                                <input class='rules_all' type='checkbox' name='dept_ids[]' value='" . $oneInfo['id'] . "' $checkbox_select >
                                <i class='fa $icon'></i> " . $oneInfo['name'] . "";
                $html_view .= "<ul>";
                $html_view .= $this->tenDeptTreeCheckboxView($oneInfo[$child], $child, $rules_array);
                $html_view .= "</ul>";
                $html_view .= "</li>";
            } else {
                $html_view .= "<li class='dont-check-parent'>";
                $html_view .= "<span><i></i></span>
                                <input type='checkbox' name='dept_ids[]' value='" . $oneInfo['id'] . "'  $checkbox_select >
                                <i class='fa $icon'></i> " . $oneInfo['name'] . "";
                $html_view .= "</li>";
            }
        }
        return $html_view;
    }
}